import router from '@/router'

import { useConfigStore } from '@/store/modules/config'
const configStore = useConfigStore()

const backHomeTime = configStore.config?.backHomeTime || 60000
let redirectTimer = null; // 定义全局定时器

/**
 * 启动跳转到首页的逻辑
 * @param {number} timeout - 超时时间，默认 180000 毫秒
 */
export function startRedirectToHome(timeout = backHomeTime) {
  clearTimeout(redirectTimer);

  // 使用Vue Router判断当前路由
  const currentRoute = router.currentRoute.value;
  const isHomePage = currentRoute.path === '/'; // 判断是否是首页
  if (isHomePage) {
    return;
  }
  redirectTimer = setTimeout(() => {
    // 重定向到首页
    configStore.setReback(true)
    // window.location.href = '/';
    // router.push('/')
    router.replace('/')
    window.history.replaceState(null, '', '/')
    window.history.pushState(null, '', '/')
  }, timeout);
}

/**
 * 停止跳转到首页的逻辑
 */
export function stopRedirectToHome() {
  clearTimeout(redirectTimer);
  redirectTimer = null;
}
